import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_heating/ui/ui.dart';

class UiImage extends StatelessWidget {
  const UiImage({
    super.key,
    required this.imageUrl,
    this.zoomable = false,
    this.fit,
    this.width,
    this.height,
    this.httpHeaders,
    this.color,
    this.colorBlendMode,
    this.alignment = Alignment.center,
    this.placeholder,
    this.onTap,
  });
  final String imageUrl;
  final bool zoomable;
  final BoxFit? fit;
  final double? width;
  final double? height;
  final Map<String, String>? httpHeaders;
  final Color? color;
  final BlendMode? colorBlendMode;
  final Alignment alignment;
  final Widget Function(BuildContext, String)? placeholder;
  final Function(String)? onTap;
  @override
  Widget build(BuildContext context) {
    return UiTap(
      onTap: () {
        onTap?.call(imageUrl);
        if (zoomable) {
          ImagePreview.show(context, url: imageUrl);
        }
      },
      child: zoomable
          ? Hero(
              tag: imageUrl,
              child: contentView(),
            )
          : contentView(),
    );
  }

  Widget contentView() {
    return CachedNetworkImage(
      imageUrl: imageUrl,
      fit: fit,
      alignment: alignment,
      width: width,
      height: height,
      httpHeaders: httpHeaders,
      color: color,
      colorBlendMode: colorBlendMode,
      placeholder: placeholder ??
          (context, string) =>
              const Center(child: CupertinoActivityIndicator()),
      errorWidget: (context, info, object) {
        return Container(
          color: Colors.grey.withOpacity(0.15),
          child: Image.asset(
            'res/images/ikons/error_image.png',
            alignment: Alignment.center,
          ),
        );
      },
    );
  }
}
